using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Data.DAO;
using Framework.Library.Data.DAL.JObject;

namespace Framework.Portal.Layouts.Admin.Components.Statistic
{
    public partial class Default : BaseControl
    {
        private static DataTable dtDatetime = new DataTable();
        public string MENUNAME = string.Empty;
        public string strParams = string.Empty;

        public Default()
        {
        }

        public Default(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dtDatetime = ManagerFactory.PortalStatisticManager.GetDateTime();
                lblCommandName.Text = MENUNAME;
            }
        }

        protected void drpThongke_SelectedIndexChanged(object sender, EventArgs e)
        {
            drpNam.Items.Clear();
            drpThang.Items.Clear();
            drpNgay.Items.Clear();
            if (drpThongke.SelectedValue == "YEAR")
            {
                DataRow[] drDatetime = dtDatetime.Select();
                var arrYEAR = new ArrayList();
                int index = 0;
                foreach (DataRow row in drDatetime)
                {
                    index = Convert.ToInt32(row["YEAR"]);
                    if (!arrYEAR.Contains(index))
                        arrYEAR.Add(index);
                }
                foreach (int indx in arrYEAR)
                {
                    var item = new ListItem(indx.ToString(), indx.ToString());
                    item.Attributes.Add("Level", "0");
                    drpNam.Items.Add(item);
                }

                btnClick.Visible = true;
                drpNam.Visible = true;
                drpThang.Visible = false;
                drpNgay.Visible = false;
            }
            else if (drpThongke.SelectedValue == "MONTH")
            {
                DataRow[] drDatetime = dtDatetime.Select();
                var arrYEAR1 = new ArrayList();
                int index = 0;
                foreach (DataRow row in drDatetime)
                {
                    index = Convert.ToInt32(row["YEAR"]);
                    if (!arrYEAR1.Contains(index))
                        arrYEAR1.Add(index);
                }
                foreach (int indx in arrYEAR1)
                {
                    var item = new ListItem(indx.ToString(), indx.ToString());
                    item.Attributes.Add("Level", "0");
                    drpNam.Items.Add(item);
                }
                DataRow[] drDatetime1 = dtDatetime.Select("YEAR=" + drpNam.SelectedValue);
                var arrMONTH = new ArrayList();
                int index1 = 0;
                foreach (DataRow row in drDatetime1)
                {
                    index1 = Convert.ToInt32(row["MONTH"]);
                    if (!arrMONTH.Contains(index1))
                        arrMONTH.Add(index1);
                }
                foreach (int indx in arrMONTH)
                {
                    var item = new ListItem(String.Format("{0:00}", indx), String.Format("{0:00}", indx));
                    item.Attributes.Add("Level", "0");
                    drpThang.Items.Add(item);
                }
                btnClick.Visible = true;
                drpNam.Visible = true;
                drpThang.Visible = true;
                drpNgay.Visible = false;
            }
            else if (drpThongke.SelectedValue == "DAY")
            {
                DataRow[] drDatetime = dtDatetime.Select();
                var arrYEAR2 = new ArrayList();
                int index = 0;
                foreach (DataRow row in drDatetime)
                {
                    index = Convert.ToInt32(row["YEAR"]);
                    if (!arrYEAR2.Contains(index))
                        arrYEAR2.Add(index);
                }
                foreach (int indx in arrYEAR2)
                {
                    var item = new ListItem(indx.ToString(), indx.ToString());
                    item.Attributes.Add("Level", "0");
                    drpNam.Items.Add(item);
                }

                DataRow[] drDatetime1 = dtDatetime.Select("YEAR=" + drpNam.SelectedValue);
                var arrMONTH1 = new ArrayList();
                int index1 = 0;
                foreach (DataRow row in drDatetime1)
                {
                    index1 = Convert.ToInt32(row["MONTH"]);
                    if (!arrMONTH1.Contains(index1))
                        arrMONTH1.Add(index1);
                }
                foreach (int indx in arrMONTH1)
                {
                    var item = new ListItem(String.Format("{0:00}", indx), String.Format("{0:00}", indx));
                    item.Attributes.Add("Level", "0");
                    drpThang.Items.Add(item);
                }

                DataRow[] drDatetime2 = dtDatetime.Select("MONTH=" + drpThang.SelectedValue);
                var arrDAY = new ArrayList();
                int index2 = 0;
                foreach (DataRow row in drDatetime2)
                {
                    index2 = Convert.ToInt32(row["DAY"]);
                    if (!arrDAY.Contains(index))
                        arrDAY.Add(index2);
                }
                foreach (int indx in arrDAY)
                {
                    var item = new ListItem(String.Format("{0:00}", indx), String.Format("{0:00}", indx));
                    item.Attributes.Add("Level", "0");
                    drpNgay.Items.Add(item);
                }
                btnClick.Visible = true;
                drpNam.Visible = true;
                drpThang.Visible = true;
                drpNgay.Visible = true;
            }
            else
            {
                btnClick.Visible = false;
                drpNam.Visible = false;
                drpThang.Visible = false;
                drpNgay.Visible = false;
            }
        }

        protected void drpNam_SelectedIndexChanged(object sender, EventArgs e)
        {
            drpThang.Items.Clear();
            drpNgay.Items.Clear();
            DataRow[] drDatetime1 = dtDatetime.Select("YEAR=" + drpNam.SelectedValue);
            foreach (DataRow row in drDatetime1)
            {
                var item = new ListItem(row["MONTH"].ToString(), row["MONTH"].ToString());
                item.Attributes.Add("Level", "0");
                drpThang.Items.Add(item);
            }
            DataRow[] drDatetime2 = dtDatetime.Select("MONTH=" + drpThang.SelectedValue);
            foreach (DataRow row in drDatetime2)
            {
                var item = new ListItem(row["DAY"].ToString(), row["DAY"].ToString());
                item.Attributes.Add("Level", "0");
                drpNgay.Items.Add(item);
            }
        }

        protected void drpThang_SelectedIndexChanged(object sender, EventArgs e)
        {
            drpNgay.Items.Clear();
            DataRow[] drDatetime2 = dtDatetime.Select("MONTH=" + drpThang.SelectedValue);
            foreach (DataRow row in drDatetime2)
            {
                var item = new ListItem(row["DAY"].ToString(), row["DAY"].ToString());
                item.Attributes.Add("Level", "0");
                drpNgay.Items.Add(item);
            }
        }

        protected void btnClick_Click(object sender, EventArgs e)
        {
            strParams = string.Empty;
            pnlChart.Visible = true;
            pnlDetailInfo.Visible = true;
            pnlTotalInfo.Visible = true;
            DataTable dt = null;
            if (drpThongke.SelectedValue == "YEAR")
            {
                dt = ManagerFactory.PortalStatisticManager.ChartYearStatistic(Session.WorkingPortal().PORTALID.ToString(),
                                                                              drpNam.SelectedValue);
                strParams = BuildParams(12, dt);
                rptTotalInfo.DataSource =
                    ManagerFactory.PortalStatisticManager.TotalYearStatistic(Session.WorkingPortal().PORTALID.ToString(),
                                                                             drpNam.SelectedValue);
                rptDetailInfo.DataSource =
                    ManagerFactory.PortalStatisticManager.DetailYearStatistic(Session.WorkingPortal().PORTALID.ToString(),
                                                                              drpNam.SelectedValue);
                rptTotalInfo.DataBind();
                rptDetailInfo.DataBind();
            }
            else if (drpThongke.SelectedValue == "MONTH")
            {
                dt = ManagerFactory.PortalStatisticManager.ChartMonthStatistic(Session.WorkingPortal().PORTALID.ToString(),
                                                                               drpThang.SelectedValue + "/" +
                                                                               drpNam.SelectedValue);
                int nums = DateTime.DaysInMonth(Convert.ToInt32(drpNam.SelectedValue),
                                                Convert.ToInt32(drpThang.SelectedValue));
                strParams = BuildParams(nums, dt);
                rptTotalInfo.DataSource =
                    ManagerFactory.PortalStatisticManager.TotalMonthStatistic(Session.WorkingPortal().PORTALID.ToString(),
                                                                              drpThang.SelectedValue + "/" +
                                                                              drpNam.SelectedValue);
                rptDetailInfo.DataSource =
                    ManagerFactory.PortalStatisticManager.DetailMonthStatistic(Session.WorkingPortal().PORTALID.ToString(),
                                                                               drpThang.SelectedValue + "/" +
                                                                               drpNam.SelectedValue);
                rptTotalInfo.DataBind();
                rptDetailInfo.DataBind();
            }
            else if (drpThongke.SelectedValue == "DAY")
            {
                dt = ManagerFactory.PortalStatisticManager.ChartDayStatistic(Session.WorkingPortal().PORTALID.ToString(),
                                                                             drpNgay.SelectedValue + "/" +
                                                                             drpThang.SelectedValue + "/" +
                                                                             drpNam.SelectedValue);
                strParams = BuildParams(24, dt);
                rptTotalInfo.DataSource =
                    ManagerFactory.PortalStatisticManager.TotalDayStatistic(Session.WorkingPortal().PORTALID.ToString(),
                                                                            drpNgay.SelectedValue + "/" +
                                                                            drpThang.SelectedValue + "/" +
                                                                            drpNam.SelectedValue);
                rptDetailInfo.DataSource =
                    ManagerFactory.PortalStatisticManager.DetailDayStatistic(Session.WorkingPortal().PORTALID.ToString(),
                                                                             drpNgay.SelectedValue + "/" +
                                                                             drpThang.SelectedValue + "/" +
                                                                             drpNam.SelectedValue);
                rptTotalInfo.DataBind();
                rptDetailInfo.DataBind();
            }
        }

        protected void rptTotalInfo_DatalistBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var row = (DataRowView) e.Item.DataItem;
                var lblQuocgia = (Label) e.Item.FindControl("lblQuocgia");
                var lblTong = (Label) e.Item.FindControl("lblTong");

                lblQuocgia.Text = Convert.ToString(row[0]);
                lblTong.Text = Convert.ToString(row[1]);
            }
        }

        protected void rptDetailInfo_DatalistBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var row = (DataRowView) e.Item.DataItem;
                var lblIP = (Label) e.Item.FindControl("lblIP");
                var lblQuocgia = (Label) e.Item.FindControl("lblQuocgia");
                var lblDatetime = (Label) e.Item.FindControl("lblDatetime");

                lblDatetime.Text = Convert.ToDateTime(row[2]).ToString("dd/MM/yyyy HH:mm:ss");
                lblIP.Text = Convert.ToString(row[0]);
                lblQuocgia.Text = Convert.ToString(row[1]);
            }
        }

        private string BuildParams(int nums, DataTable dt)
        {
            string retVal = string.Empty;
            var arrParams = new string[nums];
            foreach (DataRow row in dt.Rows)
            {
                arrParams[Convert.ToInt32(row[0])] = row[1].ToString();
            }
            for (int i = 0; i < arrParams.Length; i++)
            {
                if (i == 0)
                {
                    if (!string.IsNullOrEmpty(arrParams[i]))
                        retVal = arrParams[i];
                    else
                        retVal = "0";
                }
                else
                {
                    if (!string.IsNullOrEmpty(arrParams[i]))
                        retVal += "," + arrParams[i];
                    else
                        retVal += ",0";
                }
            }
            return retVal;
        }
    }
}